<?php

namespace Addons\Slide\Controller;
use Home\Controller\AddonsController;

class SlideController extends AddonsController{
    /**
     * 保存轮播图的方法
     */
    public function add(){
        if(IS_POST){
           // var_dump(I('post.'));
            $res=I('post.');
            $a=M('slide')->add($res);
            if($a){
                $this->success('保存成功');

            }else{
                $this->error('保存失败');
            }
        }else{
            $this->meta_title = '添加轮播图链接';
            $this->display(T('Addons://Slide@Slide/add'));
        }

    }

    /**
     * 某个轮播图的图片列表
     */
    public function pic(){
        $id=I('get.id');
       $res =M('Slides')->alias('s')->join(C("DB_PREFIX").'picture p ON s.img = p.id')->where('s.pid='.$id)->field('s.id,s.href,s.info,p.path')->select();
       //var_dump($res);die;
        $this->meta_title = '图片管理';
        $this->assign('list',$res);
        $this->assign('id',$id);
        $this->display(T('Addons://Slide@Slide/pic'));


    }

    /**
     * 添加图片
     */
    public function picadd(){
        if(IS_POST){
                //var_dump(I('post.'));
                $res=I('post.');
                $a=M('Slides')->add($res);
                if($a){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
        }else{
            $pid=I('get.id');
            $this->meta_title = '添加图片';
            $this->assign('pid',$pid);
            $this->display(T('Addons://Slide@Slide/picadd'));
        }
    }

    /**
     * 删除某个轮播图
     */
    public function del(){
        $id=I('get.id');
        //var_dump($id);
        $a=M('slide')->where('id='.$id)->delete();
        $b=M('slides')->where('pid='.$id)->delete();
        if($a && $b){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 轮播图的编辑操作
     */
    public function edit(){
        if(IS_POST){
           // var_dump(I('post.'));
            $id=I('post.id');
            $data=I('post.c');
            if($id==''){
                $this->error('索引出错');
            }
            $a=M('slide')->where('id='.$id)->save($data);
            if($a==1){
                $this->success('保存成功');

            }elseif ($a == 0){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $id=I('get.id');
            $res=M('slide')->where('id='.$id)->find();
            if(!$res){
                $this->error('查询出错');
            }
            //var_dump($res);die;
            $this->assign('list',$res);
            $this->display(T('Addons://Slide@Slide/edit'));
            var_dump($res);
        }

    }

    /**
     * 轮播图的禁止操作
     */
    public function forbidden(){
       // var_dump(I('get.id'));
        $id=I('get.id');
        $a=M('slide')->where('id='.$id)->setField('status',0);
        //var_dump($a);die;
        if($a){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

    /**
     * 轮播图的开启操作
     */
    public function off(){
        $id=I('get.id');
        $a=M('slide')->where('id='.$id)->setField('status',1);
        //var_dump($a);die;
        if($a){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }


    /**
     * 轮播图的批量操作(启动，禁止)
     */
    public function savestatus(){
        //var_dump(I('post.id'));
        //var_dump(I('get.status'));
        $data=I('post.id');
        $status=I('get.status');
        //var_dump($data);die;
       // M('slide')->where();
        if($status==1){
            foreach ($data as $v){

                $a=M('slide')->where('id='.$v)->setField('status',1);
            }

            if($a==1){
                $this->success('保存成功');

            }elseif ($a == 0){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            foreach ($data as $v){

                $a=M('slide')->where('id='.$v)->setField('status',0);
            }

            if($a==1){
                $this->success('保存成功');

            }elseif ($a == 0){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
    }

    /**
     * 单个图片的删除
     */
    public function delpic(){
        $id=I('get.id');
        //var_dump($id);
        $a=M('slides')->where('id='.$id)->delete();
        if($a){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 图片的编辑
     */
    public function editpic(){
            if(IS_POST){
               // var_dump(I('post.'));
                $id=I('post.id');
                $data=I('post.c');
                $a=M('slides')->where('id='.$id)->save($data);
                if($a==1){
                    $this->success('保存成功');

                }elseif ($a == 0){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }else{
            $id=I('get.id');
            $data=M('slides')->where('id='.$id)->find();
           // var_dump($data);die;
            $this->assign('list',$data);

                $this->display(T('Addons://Slide@Slide/editpic'));
            }
    }

    /**
     * 图片的批量删除
     */
    public function picdels(){
        $data=I('post.id');
        $status=I('get.status');
       // var_dump($data);
       // var_dump($status);
        if($status==1){
            foreach ($data as $v) {
                $a=M('slides')->where('id='.$v)->delete();
            }
            if($a){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }

        }
    }
}
